//https://leetcode.cn/problems/three-steps-problem-lcci/

class Solution {
public:
    int waysToStep(int n) {
        //1.创建dp表
        //2.初始化
        //3.填表
        //4.返回值

        if (n == 1 || n == 2) return n;
        if (n == 3) return 4;

        vector<int> dp(n + 1, 0);
        dp[1] = 1, dp[2] = 2, dp[3] = 4;

        int MOD = 1e9 + 7;
        for (int i = 4; i <= n; i++)
        {
            dp[i] = ((dp[i - 1] + dp[i - 2]) % MOD + dp[i - 3]) % MOD;
        }

        return dp[n] % MOD;

    }
};

//空间优化
class Solution {
public:
    int waysToStep(int n) {
        //1.创建dp表
        //2.初始化
        //3.填表
        //4.返回值

        if (n == 1 || n == 2) return n;
        if (n == 3) return 4;

        int a = 1, b = 2, c = 4, d = 0;
        int MOD = 1e9 + 7;
        for (int i = 4; i <= n; i++)
        {
            d = ((a + b) % MOD + c) % MOD;
            a = b;
            b = c;
            c = d;
        }

        return d % MOD;

    }
};